﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web.Script.Serialization;

namespace DVK.SP2007
{
    /// <summary>
    /// The DvkMapping class is used to map SharePoint field information with DvkElement information.
    /// <remarks>
    /// The DvkMapping is serialized to JSON and deserialized from JSON.
    /// The mapping is held in SharePoint (metadata mapping list) in the form of JSON.
    /// </remarks>
    /// <code>
    /// Xml format:
    /// &lt;Mapping 
    ///   SPFieldName="DokumendiNr" DvkElementName="SenderIdentifier"
    ///   DvkNamespace="http://www.riigikantselei.ee/schemas/letter"
    ///   ParentElement="LetterMetaData" 
    ///   ParentNamespace="http://www.riik.ee/schemas/dhl"
    /// /&gt;
    ///</code>
    ///<code>
    /// JSON format:
    /// {"SPFieldName":"DokumendiNr", "DvkElementName"="SenderIdentifier", "DvkNamespace"="http://www.riigikantselei.ee/schemas/letter", "ParentElement"="LetterMetaData"}
    /// </code>
    /// </summary>
    [Serializable]
    public class DvkMapping
    {
        /// <summary>
        /// Gets or sets the name of the SP field.
        /// </summary>
        /// <value>The name of the SP field.</value>
        public string SPFieldName { get; set; }
        /// <summary>
        /// Gets or sets the name of the dvk element.
        /// </summary>
        /// <value>The name of the element.</value>
        public string ElementName { get; set; }
        /// <summary>
        /// Gets or sets the dvk element namespace.
        /// </summary>
        /// <value>The namespace.</value>
        public string Namespace { get; set; }
        /// <summary>
        /// Gets or sets the name of the parent element.
        /// </summary>
        /// <value>The name of the parent element.</value>
        public string ParentElementName { get; set; }
        /// <summary>
        /// Gets or sets the parent element namespace.
        /// </summary>
        /// <value>The parent namespace.</value>
        public string ParentNamespace { get; set; }

        /// <summary>
        /// Gets the name of the DVK element.
        /// </summary>
        /// <value>The name of the DVK element.</value>
        [ScriptIgnore]
        public XName DvkElementName
        {
            get
            {
                return XName.Get(ElementName, Namespace);
            }
        }
        /// <summary>
        /// Gets the name of the DVK parent element.
        /// </summary>
        /// <value>The name of the DVK parent element.</value>
        [ScriptIgnore]
        public XName DvkParentElementName
        {
            get
            {
                return XName.Get(ParentElementName, ParentNamespace);
            }
        }
    }
}
